import loginViews from '$v/login'

//引入登录请求
import { loginRequest } from '$a/admin'
export default (req, res) => {
  //响应
  res.render(loginViews())

  //点击登录逻辑
  document.loginForm.loginBtn.onclick = async function (e) {

    //阻止浏览器自动刷新
    e.preventDefault()
    //获取用户输入的内容
    const adminNameValue = document.loginForm.adminName.value;
    const passwordValue = document.loginForm.passWord.value;
    
    
    
    try {
      const loginResult = await loginRequest({
        adminName: adminNameValue,
        password: passwordValue
      })
      //登录成功可以拿到token,把token保存在localStorage中,并跳转到首页
      localStorage.setItem("user_token", loginResult.token)
      router.go('/adv/adminList')
    } catch (e) {
      alert('登录错误' + e.message)
    }
  }
}